package com.example.myapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.viewpager.widget.PagerAdapter;

import com.example.myapplication.R;
import com.example.myapplication.utils.ImageLoader;

public class BannerAdapter extends PagerAdapter {

    private Context context;
    private int[] bannerImages; // 你的广告图片资源

    public BannerAdapter(Context context, int[] bannerImages) {
        this.context = context;
        this.bannerImages = bannerImages;
    }

    @Override
    public int getCount() {
        return bannerImages.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.banner_item, container, false);

        ImageView imageView = view.findViewById(R.id.iv_banner);

        // 使用ImageLoader加载图片
        ImageLoader.load(context, String.valueOf(bannerImages[position]), imageView, R.mipmap.ic_launcher, R.mipmap.ic_launcher);

        container.addView(view);

        return view;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
